﻿using System.IO;
using UnityEditor;
using UnityEngine;




namespace Sailfish
{

  
    public class CreatEditor : Editor
    {
        static string resPath             =>  CreatEditorData.instance.resPath;
        static string streamPath          =>  CreatEditorData.instance.streamPath;
        static string addressablePath     =>  CreatEditorData.instance.addressablePath;

        static string saveResPath         =>  $"{System.Environment.CurrentDirectory}/{CreatEditorData.instance.saveResPath}";
        static string saveTagsPath        =>  $"{System.Environment.CurrentDirectory}/{CreatEditorData.instance.saveTagsPath}";
        static string saveStreamPath      =>  $"{System.Environment.CurrentDirectory}/{CreatEditorData.instance.saveStreamPath}";
        static string saveAddressablePath =>  $"{System.Environment.CurrentDirectory}/{CreatEditorData.instance.saveAddressablePath}";


        [MenuItem("Assets/Lr/Save Res*Stream folder #s", false, 10)]
        static void CreatFolder()
        {
            CreatResourcesFolder();
            CreatStreamAssetsFolder();

            AssetDatabase.Refresh();
        }

        [MenuItem("Assets/Lr/Save Resource folder ", false, 10)]
        static void CreatResourcesFolder()
        {
            Debug.Log(CreatEditorData.instance.resPath);

            var resCs = CreatUtil.CreatResClass(resPath);


            if(!Directory.Exists(resPath)) Directory.CreateDirectory(Path.GetDirectoryName(saveResPath));

            if (File.Exists(saveResPath))  File.Delete(saveResPath);
            File.WriteAllText(saveResPath, resCs.builder.ToString());

            AssetDatabase.Refresh();
        }

        [MenuItem("Assets/Lr/Save StreamAssets folder ", false, 10)]
        static void CreatStreamAssetsFolder()
        {
            var streamCs = CreatUtil.CreatStreamClass(streamPath);
            if (File.Exists(saveStreamPath)) File.Delete(saveStreamPath);
            File.WriteAllText(saveStreamPath, streamCs.builder.ToString());

            AssetDatabase.Refresh();
        }

        [MenuItem("Assets/Lr/Save Tags And Layer ", false, 10)]
        static void AutoTagManager()
        {
            var auto = new AutoTagAndLayer();


            var lastID = saveTagsPath.LastIndexOf('/');
            var dirPath = saveTagsPath.Substring(0, lastID);

            if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);

            if (File.Exists(saveTagsPath)) File.Delete(saveTagsPath);


            File.WriteAllText(saveTagsPath, auto.builder.ToString());

            AssetDatabase.Refresh();
        }
    }
}


